package com.gm.commonlib.base.baseAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private Object attachedHolder;
    private Object attached;

    protected ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray();
        this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        this.mConvertView.setTag(this);
    }

    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        } else {
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.mPosition = position;
            return viewHolder;
        }
    }

    public View getConvertView() {
        return this.mConvertView;
    }

    public <T extends View> T getView(int viewId) {
        View view = (View) this.mViews.get(viewId);
        if (view == null) {
            view = this.mConvertView.findViewById(viewId);
            this.mViews.put(viewId, view);
        }

        return (T) view;
    }

    public ViewHolder setText(int viewId, CharSequence text) {
        if (text == null || "".equals(text)) {
            text = "";
        }

        TextView view = (TextView) this.getView(viewId);
        view.setText((CharSequence) text);
        return this;
    }

    public ViewHolder setLeftText(int viewId, String text) {
        if (text == null || "".equals(text)) {
            text = "";
        }

        TextView textView = this.getView(viewId);
        textView.setText(text);
        return this;
    }

    public ViewHolder setCenterText(int viewId, String text) {
        if (text == null || "".equals(text)) {
            text = "";
        }

        TextView textView = this.getView(viewId);
        textView.setText(text);
        return this;
    }

    public ViewHolder setTextColorRes(int viewId, int colorId) {
        if (colorId > 0) {
            TextView view = (TextView) this.getView(viewId);
            view.setTextColor(view.getResources().getColor(colorId));
        }

        return this;
    }

    public ViewHolder setTextColor(int viewId, int color) {
        TextView view = (TextView) this.getView(viewId);
        view.setTextColor(color);
        return this;
    }

    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = (ImageView) this.getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = (ImageView) this.getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }



    public int getPosition() {
        return this.mPosition;
    }

    public <T> T getAttached() {
        return (T) this.attached;
    }

    public <T> void setAttached(T attached) {
        this.attached = attached;
    }

    public Object getAttachedHolder() {
        return this.attachedHolder;
    }

    public void setAttachedHolder(Object attachedHolder) {
        this.attachedHolder = attachedHolder;
    }
}
